package com.fr.analyzer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class WordAnalyzer {
	private static final String sourcePath = "E:\\dest.txt";

	private static WordNode _node = getInstance();

	private static WordNode getInstance() {
		if (_node == null) {
			init();
		}
		return _node;
	}

	private static void init() {
		_node = new WordNode();
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(sourcePath);
			br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				String[] s = line.split("\t");
				if (s.length >= 6) {
					addWord(s);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void addWord(String[] s) {
		for (int i = 3; i < s.length; i++) {
			addWord(s, i);
		}
	}

	private static void addWord(String[] s, int i) {
		if (StringUtils.isNotBlank(s[i])) {
			WordNode tmp = _node;
			for (char c : s[i].toLowerCase().toCharArray()) {
				WordNode w = tmp.sub.get(c);
				if (w == null) {
					w = new WordNode();
					w.c = c;
					tmp.sub.put(c, w);
				}
				tmp = w;
			}
			tmp.id = s[0];
			tmp.parentId = s[1];
			tmp.type = s[2];
			tmp.wholeWord = s[i];
			tmp.isEnd = true;
		}
	}

	public static List<WordNode> analyzer(String text) {
		if (StringUtils.isBlank(text)) {
			return Collections.emptyList();
		}
		int length = text.length();
		int begin = 0;
		int point = 0;
		int end = 0;
		WordNode current = _node;
		List<WordNode> rs = new ArrayList<>();
		WordNode tmp = null;
		while (begin < length) {
			if(current == null){
				if(tmp == null){
					begin ++;
				}else{
					rs.add(tmp);
					begin = end;
					tmp = null;
				}
				point = begin;
				current = _node;
				continue;
			}
			if (current.isEnd) {
				tmp = current;
				end = point;
			}
			if(point < length){
				char c = text.charAt(point);
				current = current.sub.get(c);
				point ++;
			}else{
				current = null;
			}
		}
		return rs;
	}
	
	public static void main(String[] args) {
//		getInstance();
		_node = new WordNode();
		String [][] s= {{"","","","杭州市"},{"","","","西湖区"}};
		addWord(s[0], 3);
		addWord(s[1], 3);
//		String [] test = {"丹霞山和景温泉","杭州市西湖区","北戴河野生动物园","北戴河野生","杭州西湖","hangzhou","洪泽县"};
		String [] test = {"杭州市西湖区"};
		
		for(String ss : test){
			System.out.println(analyzer(ss));
		}
	}
}
